package com.atguigu.controller;

import com.atguigu.entity.Payment;
import com.atguigu.entity.Result;
import com.atguigu.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author: mr.zhang
 * @description:
 * @create: 2021-06-21 16:41
 */
@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private Integer port;

    @PostMapping(value = "/payment/create")
    public Result<Payment> create(@RequestBody Payment payment){ //埋雷
        int result = paymentService.create(payment);
        log.info("*****插入结果："+result);
        if (result>0){  //成功
            return new Result(200,"插入数据库成功port: "+port,result);
        }else {
            return new Result<>(444,"插入数据库失败",null);
        }
    }
    @GetMapping(value = "/payment/get/{id}")
    public Result<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果："+payment);
        if (payment!=null){  //说明有数据，能查询成功
            return new Result<>(200,"查询成功port: "+port,payment);
        }else {
            return new Result<>(444,"没有对应记录，查询ID："+id,null);
        }
    }


    @GetMapping(value = "/payment/timeout")
    public String paymentFeignTimeout() {
        try {
            System.out.println(port);
            TimeUnit.SECONDS.sleep(3);
        } catch (Exception e) {
            e.printStackTrace();
        } //单位秒
        return port + "";
    }
}
